package com.zkx.laolaiyi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zkx.laolaiyi.constant.MqExchangeConstants;
import com.zkx.laolaiyi.constant.RedisConstants;
import com.zkx.laolaiyi.constant.ResultCodeConstant;
import com.zkx.laolaiyi.constant.ResultMsgConstant;
import com.zkx.laolaiyi.dto.UserDTO;
import com.zkx.laolaiyi.entity.LoginUser;
import com.zkx.laolaiyi.entity.User;
import com.zkx.laolaiyi.result.Result;
import com.zkx.laolaiyi.service.IUserService;
import com.zkx.laolaiyi.service.LoginService;
import jakarta.annotation.PostConstruct;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {
    private final LoginService loginService;
    private final IUserService userService;
//    private final RedissonClient redissonClient;
//    private final StringRedisTemplate stringRedisTemplate;
//    private final PasswordEncoder passwordEncoder;
//    private final RabbitTemplate rabbitTemplate;
//    private static final DefaultRedisScript<Long> REDIS_SCRIPT;
//    private static final ExecutorService REGISTER_EXECUTOR = Executors.newFixedThreadPool(10);

//    static {
//        REDIS_SCRIPT = new DefaultRedisScript<>();
//        REDIS_SCRIPT.setLocation(new ClassPathResource("lua/set_register_uid.lua"));
//        REDIS_SCRIPT.setResultType(Long.class);
//    }

//    @PostConstruct
//    private void init() {
//
//    }

    /**
     * 用户登录接口
     * @param userDTO
     * @return
     */
    @PostMapping("/login")
    public Result<LoginUser> login(@RequestBody UserDTO userDTO) {
        log.info("用户开始登录");
//        Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userDTO.getUsername());
        return loginService.login(userDTO);
    }

    /**
     * 用户注册接口
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserDTO userDTO) {
        log.info("用户开始注册");
        return userService.register(userDTO);
    }
}
